package com.ruicar.afs.cloud.manage.workflow.processor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.modules.contract.enums.ProcessStatusEnum;
import com.ruicar.afs.cloud.manage.workflow.entity.WorkTaskAssignInfo;
import com.ruicar.afs.cloud.manage.workflow.service.WorkTaskAssignInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 统一处理是否需要在页面展示 处理 按钮
 */
@Component
@Slf4j
@AllArgsConstructor
public class ApproveTaskProcessor {

    private final WorkTaskAssignInfoService workTaskAssignInfoService;

    public JSONArray handle(List list) {
        JSONArray ja = new JSONArray();
        String userRealName = SecurityUtils.getUser().getUserRealName();
        String userName = SecurityUtils.getUser().getUsername();
        List<String> roleCodeList = SecurityUtils.getUser().getRoleCodeList();
        List<WorkTaskAssignInfo> assignInfo = workTaskAssignInfoService.list(Wrappers.<WorkTaskAssignInfo>lambdaQuery()
                .and(wraper -> wraper
                        .eq(WorkTaskAssignInfo::getUserName, userName)
                        .or()
                        .in(WorkTaskAssignInfo::getUserName, roleCodeList)
                )
        );
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(list.get(i)));

            String id = String.valueOf(jsonObject.get("id"));
            jsonObject.put("isViewBtn", false);
            for (WorkTaskAssignInfo info : assignInfo) {
                if (info.getBizDataId().equals(id)) {
                    jsonObject.put("isViewBtn", true);
                }
            }
            ja.add(jsonObject);
        }
        return ja;
    }


}
